<template>
  <div class="pageDetail">
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="pageForm.title" placeholder="请输入服务标题" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceTypeId">
        <el-select v-model="pageForm.serviceTypeId" placeholder="请选择服务类型" :disabled="disabled">
          <el-option 
            v-for="(item,index) in service_type" 
            :label="item.dictLabel" 
            :value="item.dictCode"
            :key="index" 
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="服务对象" prop="targetGroupId">
        <el-select v-model="pageForm.targetGroupId" placeholder="请选择服务对象" :disabled="disabled">
          <el-option 
            v-for="(item,index) in target_group_type" 
            :label="item.dictLabel" 
            :value="item.dictCode"
            :key="index" 
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="director">
        <el-input v-model="pageForm.director" placeholder="请输入负责人" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="服务周期" prop="startDate">
        <el-date-picker
          unlink-panels
          v-model="dateTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :disabled="disabled"
          @change="(val) => changeDateRange(val)"
        />
      </el-form-item>
      <el-form-item label="成员" prop="members">
        <el-input 
          v-model="pageForm.members" 
          placeholder="请输入成员名单，多个用逗号分隔" 
          :disabled="disabled"
        ></el-input>
      </el-form-item>
      <el-form-item label="成果形式" prop="resultTypeId">
        <el-select v-model="pageForm.resultTypeId" placeholder="请选择成果形式" :disabled="disabled">
          <el-option 
            v-for="(item,index) in social_result_type" 
            :label="item.dictLabel" 
            :value="item.dictCode"
            :key="index" 
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="服务地点" prop="location">
        <el-input v-model="pageForm.location" placeholder="请输入服务地点" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="受益人数" prop="beneficiaryCount">
        <el-input-number 
          v-model="pageForm.beneficiaryCount" 
          placeholder="请输入受益人数" 
          :disabled="disabled"
          :min="0"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="服务时长" prop="duration">
        <el-input v-model="pageForm.duration" placeholder="请输入服务时长" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="经济效益">
        <el-input 
          type="textarea" 
          v-model="pageForm.economicBenefit" 
          placeholder="请输入经济效益描述" 
          :disabled="disabled"
        ></el-input>
      </el-form-item>
      <el-form-item label="成果附件" prop="attachmentFileId">
        <FileUpload v-model="pageForm.attachmentFileId" :disabled="disabled" :fileInfo="pageForm.attachmentFileList"/>
      </el-form-item>
      <el-form-item label="内容描述">
        <el-input 
          type="textarea" 
          v-model="pageForm.contentDescription" 
          placeholder="请输入内容描述（200字以内）" 
          :disabled="disabled"
          maxlength="200"
        ></el-input>
      </el-form-item>
      <el-form-item label="技术方案摘要" prop="technicalSummary">
        <el-input 
          type="textarea" 
          v-model="pageForm.technicalSummary" 
          placeholder="请输入技术方案摘要（200字以内）" 
          :disabled="disabled"
          maxlength="200"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="handleSubmit()" v-if="['add','edit'].includes(action)" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import { rules } from './data'
import { useDict } from '@/utils/dict/useDict'
import { socialServiceAddApi, socialServiceEditApi, socialServiceByIdApi } from '@/api/socialServiceManage/socialService'
import FileUpload from "@/components/FileUpload";
import { listUser } from "@/api/system/user";
export default {
  name: 'ParttimeRecordDetail',
  components: { FileUpload },
  data() {
    return {
      rules,
      userList: [],
      pageForm: {},
      loading: false,
      disabled: false,
      service_type: useDict('service_type'),
      target_group_type: useDict('target_group_type'),
      social_result_type: useDict('social_result_type'),
      serviceId: this.$route.query.serviceId,
      action: this.$route.query.action,
      dateTime: []
    }
  },

  async created() {
    if (this.serviceId) this.getById() // 加载详情数据
  },
  methods: {
    changeDateRange(value) {
      this.pageForm.startDate = value ? value[0] : '';
      this.pageForm.endDate = value ? value[1] : ''
    },
    remoteMethod(val) {
      let params = { userName: val, pageNum: 1, pageSize: 10 }
      listUser(params).then((response) => {
        if (response.code == 200) {
          this.userList = response.rows
        } else {
          this.$message.error(response.msg);
        }
      });
    },
    
    async handleSubmit() {
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        let res = {}
        if (this.serviceId) {
          delete this.pageForm.fileInfo

          res = await socialServiceEditApi(this.pageForm)
        } else {
          res = await socialServiceAddApi(this.pageForm)
        }
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$router.back()
        } else {
          this.$message.error(res.msg || '操作失败')
        }
      } catch (error) {
        this.$message.error('提交失败')
      } finally {
        this.loading = false
      }
    },

    // 获取兼职记录详情
    async getById() {
      if (this.action === 'look') this.disabled = true
      socialServiceByIdApi(this.serviceId).then(res => {
        if (res.code === 200) {
          this.pageForm = res.data;
          // 回显日期范围
          this.dateTime = [
            this.pageForm.startDate || '',
            this.pageForm.endDate || ''
          ];
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-select {
	width: 100% !important;
}
::v-deep .el-date-editor{
  width: 100% !important;
}
::v-deep .el-input-number{
  width: 100% !important;
}
</style>